package com.barneyx.mshinfoplatform.web.front.user;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.enums.EPlatformType;
import com.barneyx.mshinfoplatform.entity.user.PlatformUserInfo;
import com.barneyx.mshinfoplatform.entity.user.UserBindPhone;
import com.barneyx.mshinfoplatform.entity.user.UserEntity;
import com.barneyx.mshinfoplatform.entity.user.UserPlatformEntity;
import com.barneyx.mshinfoplatform.exception.UserException;
import com.barneyx.mshinfoplatform.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/front/user/")
@Slf4j
public class InfoUserController {
    @Resource
    private UserUtil userUtil;


    @PostMapping("/user-bind-phone")
    public ExResult<?> userBindPhone(@RequestBody UserBindPhone bindData) throws UserException {
        UserPlatformEntity userPlatformInfo = userUtil.bindPlatformAndUser(bindData);
        return ExResult.ofAuto(PlatformUserInfo.builder()
                .IsBind(StrUtil.isAllNotBlank(userPlatformInfo.getPhone(), userPlatformInfo.getUser().getUserPhone()))
                .PlatformUser(userPlatformInfo)
                .User(userPlatformInfo.getUser())
                .Device(EPlatformType.StrToE(StpUtil.getLoginDevice()))
                .build()
        );
    }

    @PostMapping("/h5-login")
    public ExResult<?> h5Login(@RequestBody JSONObject loginData) throws UserException {
        String userName = loginData.get("userName").toString().trim();
        String userPwd = loginData.get("userPwd").toString().trim();
//        String device = loginData.get("device").toString().trim();
        UserPlatformEntity user_platform_info = userUtil.createOrUpdateH5Info(userName, userPwd);

        return ExResult.ofAuto(PlatformUserInfo.builder()
                .PlatformUser(user_platform_info)
                .Device(EPlatformType.H5)
                .User(user_platform_info.getUser())
                .build());
    }

    @PostMapping("/update-platform-info")
    public ExResult updatePltafomrInfo(@RequestBody UserPlatformEntity userPlatform) throws UserException {
        userUtil.updatePlatfomrInfo(userPlatform);
        return ExResult.ofAuto(PlatformUserInfo.builder()
                .PlatformUser(userPlatform)
                .Device(EPlatformType.H5)
                .User(userPlatform.getUser())
                .build());
    }


    @PostMapping("/update-realname-info")
    public ExResult<?> updateRealNameInfo(@RequestBody UserEntity userinfo) throws UserException {
        userUtil.updateUserInfo(userinfo);
        return ExResult.ofAuto(userinfo);
    }
}
